package com.huiminpay.merchant.web;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.RestResponse;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.api.IMerchantService;
import com.huiminpay.merchant.api.SmsService;
import com.huiminpay.merchant.convert.MerchantDetailConvert;
import com.huiminpay.merchant.convert.MerchantRegisterConvert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.service.FileService;
import com.huiminpay.merchant.util.SecurityUtil;
import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName MechantController
 * @Description:
 * @Author: longlong
 * @CreateDate: 2023/11/30 15:34
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "商户管理")
@RestController
public class MechantController {
    @Reference
    IMerchantService merchantService;
    @Reference
    SmsService smsService;

    @Autowired
    FileService fileService;

    @Reference
    AppService appService;


    @ApiOperation("查询商户")
    @GetMapping("/getMerchant/{id}")
    public MerchantDTO getMerchant(@PathVariable Long id) {
        MerchantDTO dto = merchantService.queryMerchantById(id);
        return dto;
    }

    @ApiOperation("发送验证码")
    @GetMapping("/sms")
    public String getSMSCode(@RequestParam String phone) {
        log.info("向手机号:{}发送验证码", phone);
        return smsService.sendSms(phone);
    }

    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType =
            "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo merchantRegister) {
// 1.校验
        if (merchantRegister == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
//手机号非空校验
        if (StringUtils.isBlank(merchantRegister.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }
//校验手机号的合法性
        if (!PhoneUtil.isMatches(merchantRegister.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
//联系人非空校验
        if (StringUtils.isBlank(merchantRegister.getUsername())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
//密码非空校验
        if (StringUtils.isBlank(merchantRegister.getPassword())) {
            throw new BusinessException(CommonErrorCode.E_100111);
        }
//验证码非空校验
        if (StringUtils.isBlank(merchantRegister.getVerifiyCode()) ||
                StringUtils.isBlank(merchantRegister.getVerifiykey())) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }
        //校验验证码
        smsService.checkVerifiyCode(merchantRegister.getVerifiykey(),
                merchantRegister.getVerifiyCode());
        //注册商户
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegister);
        merchantService.createMerchant(merchantDTO);
        return merchantRegister;
    }

    @ApiOperation("上传照片")
    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        return fileService.upload(file);
    }

    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantInfo", value = "商户认证资料", required = true, dataType = "MerchantDetailVO", paramType = "body")
    })
    public void saveMerchant(@RequestBody MerchantDetailVO vo) {
        //解析token，取出当前登录商户的id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO dto = MerchantDetailConvert.INSTANCE.vo2dto(vo);
        merchantService.applyMerchant(merchantId, dto);
    }


    /**
     * 根据商户id 查询该商户下的应用列表
     *
     * @return java.util.List<com.huiminpay.merchant.api.dto.AppDTO>
     * @author qawine
     * @date 2021/3/24 20:31
     */
    @ApiOperation("查询商户下的应用列表")
    @GetMapping(value = "/my/apps")
    public List<AppDTO> queryMyApps() {
        Long merchantId = SecurityUtil.getMerchantId();
        return appService.queryAppByMerchant(merchantId);
    }


    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        // 从token 中获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        System.out.println(merchantId);
        MerchantDTO merchant = merchantService.queryMerchantById(merchantId);
        return merchant;
    }

}
